import { Component } from '@angular/core';
import {FormsModule} from "@angular/forms";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzAutosizeDirective, NzInputDirective} from "ng-zorro-antd/input";
import {NzSpaceComponent, NzSpaceItemDirective} from "ng-zorro-antd/space";
import {NzTooltipDirective} from "ng-zorro-antd/tooltip";
import {InterfaceService} from '../services/interface.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NgIf} from '@angular/common';

@Component({
  selector: 'app-interface',
  standalone: true,
  imports: [
    FormsModule,
    NzButtonComponent,
    NzInputDirective,
    NzSpaceComponent,
    NzTooltipDirective,
    NzSpaceItemDirective,
    NgIf,
    NzAutosizeDirective
  ],
  templateUrl: './interface.component.html',
  styleUrl: './interface.component.css'
})
export class InterfaceComponent {

  sseData = "";

  constructor(private interfaceService: InterfaceService,
              private message: NzMessageService) {}

  sseEmitterLoading = false;
  sseEmitterClick(){
    this.sseEmitterLoading = true;

    let sseSubscription = this.interfaceService.sseEmitterClick().subscribe({
      next: (data) => {
        this.sseData = this.sseData + JSON.stringify( data );
      },
      error: (err) => {
        this.message.success("连接结束: "+ JSON.stringify( err) )
        sseSubscription.unsubscribe();
        this.sseEmitterLoading = false;
      },
      complete: () => {
        this.message.success("数据读取完毕！")
        sseSubscription.unsubscribe();
        this.sseEmitterLoading = false;
      }
    })
  }

  directSseEmitterLoading = false;
  directSseEmitterClick(){
    this.directSseEmitterLoading = true;
    let sseSubscription = this.interfaceService.directorSseEmitterClick().subscribe({
      next: (data) => {
        this.sseData = this.sseData + JSON.stringify( data );
      },
      error: (err) => {
        this.message.success("连接结束: "+ JSON.stringify( err) )
        sseSubscription.unsubscribe();
        this.directSseEmitterLoading = false;
      },
      complete: () => {
        this.message.success("数据读取完毕！")
        sseSubscription.unsubscribe();
        this.directSseEmitterLoading = false;
      }
    })
  }

  webClientDirectSseEmitterLoading = false;
  webClientDirectSseEmitterClick(){
    this.webClientDirectSseEmitterLoading = true;
    let sseSubscription = this.interfaceService.webClientDirectorSseEmitterClick().subscribe({
      next: (data) => {
        this.sseData = this.sseData + JSON.stringify( data );
      },
      error: (err) => {
        this.message.success("连接结束: "+ JSON.stringify( err) )
        sseSubscription.unsubscribe();
        this.webClientDirectSseEmitterLoading = false;
      },
      complete: () => {
        this.message.success("数据读取完毕！")
        sseSubscription.unsubscribe();
        this.webClientDirectSseEmitterLoading = false;
      }
    })
  }

}
